package thirdPartyUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WebClientUtil {
    @Test
    public void webclient() throws InterruptedException {
        // 1.创建WebClient实例
        WebClient webClient = WebClient
                .builder()
                .baseUrl("http://jsonplaceholder.typicode.com/posts/1")
                .build();

        // 2.通过通过retrieve()请求并获得响应
        Mono<String> bodyToMono = webClient
                .get()
                .retrieve()
                // 将请求结果处理为String类型
                .bodyToMono(String.class);

        bodyToMono.subscribe(WebClientUtil::handleResponse);
        //为了避免测试用例主线程执行完成，导致看不到异步处理结果
        while (true){

        }
//        System.out.println("请求返回的数据内容：{}" + bodyToMono.block());
    }


    @Test
    public void flux() throws InterruptedException {
        ExcelWriter excel = EasyExcel.write("exportOutMultiBatch.xlsx", User.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheetExample").build();
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);

        WebClient webClient = WebClient.builder()
                .baseUrl("http://jsonplaceholder.typicode.com")
                .build();
        List<String> userIds = Arrays.asList("1", "2", "3");
        System.out.println("ddd 执行: "+ Thread.currentThread().getName());
        Flux.fromIterable(userIds)
//                .parallel(1)
                .flatMap(pageIndex -> webClient
                        .get()
                        .uri("/posts/" + pageIndex)
                        .retrieve()
                        .bodyToMono(String.class))
                .flatMap(x->{
                    System.out.println("ddd 执行: "+ Thread.currentThread().getName());
                    System.out.println(x);

                    ArrayList<User> users = new ArrayList<>();
                    JSONObject jsonObject = JSONObject.parseObject(x);
                    User user = new User(jsonObject.getString("id"), jsonObject.getString("title"));
                    users.add(user);
                    excel.write(users, sheet);
                    return Mono.empty();
                })
                .blockLast();
        excel.finish();

    }

    @Test
    public void dd(){
        WebClient webClient = WebClient.builder()
                .baseUrl("http://jsonplaceholder.typicode.com")
                .build();
        Flux.push(pageIndex -> webClient
                .get()
                .uri("/posts/1")
                .retrieve()
                .bodyToMono(String.class))
                .flatMap(x->{
                    System.out.println("ddd 执行: "+ Thread.currentThread().getName());
                    System.out.println(x);
                    return Mono.empty();
                })
                .blockLast();
    }
    @Test
    public void fluxPush() {
        Flux<String> created = Flux.create(s -> {
            s.next("test1");
            s.next("test2");
            s.next("test3");
            s.complete();
        });
        created.subscribe(WebClientUtil::handleResponse);
    }
    private static void handleResponse(String postDTO) {
        System.out.println("handle response:=======================");
        System.out.println("ddd 执行: "+ Thread.currentThread().getName());
        System.out.println(postDTO);
    }
    public Mono<String> getUser(String id) {
        System.out.println("Enum");
//        WebClient webClient = WebClient.create("https://www.baidu.com");
//        return webClient.get()
//                .uri("?id=", id)
//                .retrieve()
//                .bodyToMono(String.class);
        // 1.创建WebClient实例
        WebClient webClient = WebClient
                .builder()
                .baseUrl("https://www.baidu.com?id=1")
                .build();

        // 2.通过通过retrieve()请求并获得响应
        return webClient
                .get()
                .retrieve()
                // 将请求结果处理为String类型
                .bodyToMono(String.class);

    }
}

@Data
@AllArgsConstructor
class User {

    @ExcelProperty("用户名")
    private String userId;
    @ExcelProperty("生日")
    private String  title;
}